package com.java2007.hotel.controller;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.pojo.FoodTable;
import com.java2007.hotel.pojo.FoodType;
import com.java2007.hotel.service.FoodTypeService;
import com.mysql.cj.util.StringUtils;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.sun.org.apache.bcel.internal.generic.PUSH;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 处理菜系有关的请求
 *
 * @author csl
 * @version V1.0
 * @date 2021/01/22
 **/
@WebServlet("/hotel/foodType")
public class FoodTypeServlet extends BaseServlet {
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean(Constant.FOOD_TYPE_SERVICE_IMPL);

    /**
     * 查询所有菜系
     *
     * @param req
     * @param resp
     * @return
     */
    public String selectAll(HttpServletRequest req, HttpServletResponse resp) {
        HttpSession session = req.getSession();
        List<FoodType> foodTypes = null;
        try {
            foodTypes = foodTypeService.selectAllFoodType();
        } catch (SQLException e) {
            e.printStackTrace();
            return "toIndex";
        }
        session.setAttribute("foodTypes", foodTypes);
        return "redirect:" + req.getContextPath() + "/hotel/backend/detail/foodtype/foodTypeLIst.jsp";
    }

    /**
     * 增加一个菜系
     * @param req
     * @param resp
     * @return
     */
    public String add(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name").trim();
        Date date = new Date();
        int i = 0;
        Admin admin = (Admin) req.getSession().getAttribute("admin");
        FoodType foodType = new FoodType(null, name, date, date, admin.getId());
        try {
            i = foodTypeService.addFoodType(foodType);
        } catch (Exception e) {
            e.printStackTrace();
            return  "toIndex";
        }
        if (i>0){
            return "redirect:" + req.getContextPath() + "/hotel/foodType?method=selectAll";
        }else {
            return  "toIndex";
        }
    }

    /**
     * 删除一个菜系
     * @param req
     * @param resp
     * @return
     */
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        int i = 0;
        try {
            i = foodTypeService.deleteFoodTypeById(Integer.parseInt(id));
            if (i == 2){
                return "back:"  + "由于菜品中含有该菜系，禁止删除！%"+"hotel/foodType?method=selectAll";
            }
        } catch (NumberFormatException | SQLException e) {
            e.printStackTrace();
            return  "toIndex";
        }

        if (i>0){
            return "redirect:" + req.getContextPath() + "/hotel/foodType?method=selectAll";
        }else {
            return  "toIndex";
        }
    }

    /**
     * 更新前获取回显数据
     * @param req
     * @param resp
     * @return
     */
    public String update(HttpServletRequest req, HttpServletResponse resp){
        String name = req.getParameter("name").trim();
        String cid = req.getParameter("cid").trim();


        int i = 0;
        try {
            i = foodTypeService.updateFoodType(name, Integer.parseInt(cid));

        } catch (NumberFormatException e) {
            e.printStackTrace();
            return  "toIndex";
        }
        if (i>0){
            return "redirect:" + req.getContextPath() + "/hotel/foodType?method=selectAll";
        }else {
            return  "toIndex";
        }

    }

    /**
     * 修改菜系的回显
     * @param req
     * @param resp
     * @return
     */
    public String beforeUpdate(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id").trim();
        System.out.println("=========="+id);
        HttpSession session = req.getSession();
        try {
            FoodType foodTypeServlet = foodTypeService.selectFoodTypeById(Integer.parseInt(id));
            session.setAttribute("foodTypeServlet",foodTypeServlet);
        } catch (SQLException e) {
            e.printStackTrace();
            return  "toIndex";
        }
        return "redirect:" + req.getContextPath() + "/hotel/backend/detail/foodtype/foodTypeUpdate.jsp";

    }

    /**
     * 搜索菜系
     * @param req
     * @param resp
     * @return
     */
    public String search(HttpServletRequest req, HttpServletResponse resp){
        String keyword = req.getParameter("keyword").trim();
        HttpSession session = req.getSession();
        if (!StringUtils.isNullOrEmpty(keyword)){
            List<FoodType> foodTypes = null;
            try {
                foodTypes = foodTypeService.selectFoodTypeByName(keyword);
            } catch (SQLException e) {
                e.printStackTrace();
                return  "toIndex";
            }
            session.setAttribute("foodTypes", foodTypes);
            return "redirect:" + req.getContextPath() + "/hotel/backend/detail/foodtype/foodTypeLIst.jsp";
        }else {
            return  "toIndex";
        }
    }

    /**
     * 查询菜系是否存在
     * @param req
     * @param resp
     * @return
     */
    public String isbName(HttpServletRequest req, HttpServletResponse resp){
        String keyword = req.getParameter("bName").trim();
        if (!StringUtils.isNullOrEmpty(keyword)){

            try {
                FoodType foodType = foodTypeService.selectFoodTypeByNameOne(keyword);
                System.out.println(foodType == null);
                if (foodType != null){
                    return "backText:"+"err";
                }else {
                    return "backText:"+"success";
                }
            } catch (SQLException e) {
                e.printStackTrace();
                return "toIndex";
            }

        }else {
            return "backText:"+"wu";
        }

    }
}
